package com.jr.dao.impl;

import com.jr.dao.IWorkOrderDao;
import com.jr.pojo.WorkOrder;
import com.jr.util.DBHelper;
import com.jr.util.PageHelper;
import com.jr.vo.WorkOrderProject;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class WorkOrderDaoImpl implements IWorkOrderDao {

    DBHelper db=new DBHelper();

    @Override
    public int insertWorkOrder(WorkOrder wo) {
        String sql="insert into workorder values(null,?,?,?,?,?)";
        Object[] objs={wo.getPid(),wo.getExecutor(),wo.getDescription(),wo.getOrderlevel(),wo.getCreatedate()};
        return db.dtUpdate(sql,objs);
    }

    @Override
    public int selectPageDateCount() {
        StringBuilder sql=new StringBuilder("select count(id) from workorder");

        Connection con=null;
        PreparedStatement ps=null;
        ResultSet resultSet=null;
        try{
            con=db.getCon();
            ps=con.prepareStatement(sql.toString());
            resultSet=ps.executeQuery();
            if (resultSet.next()){
                return resultSet.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            db.closeAll(con,ps,resultSet);
        }

        return 0;
    }

    @Override
    public ArrayList<WorkOrderProject> selectIndexDate(PageHelper ph) {
        String sql="select w.id,p.pname,w.executor,w.description,w.orderlevel,w.createdate" +
                " from workorder w inner join project p on w.pid=p.pid limit ?,?";
        int index=(ph.getPageIndex()-1)*ph.getShowNumber();
        return db.dtQuery(sql,WorkOrderProject.class,index,ph.getShowNumber());
    }
}
